# include <stdio.h>
# include <stdlib.h>
# include <string.h>

long fac (long n) {
	long sum = 1;
	long i;
	for (i=2;i<=n;i++) sum *= i;
	return sum;
}

long comb (long n, long p) {
	long num = fac(n);
	long den = fac(p)*fac(n-p);
	return (num/den);
}
void espacos (long n) {
	long i;
	for (i=0;i<(n);i++) printf("\t");
}

long calcula (long t, long u, long h, long m) {
	long i;
	long sum = 0;
	long min;
	if (u > (h*m)) {
		espacos(t-h+1);
		printf("f(%ld,%ld,%ld) = 0\n",u,m,h);
		return 0;
	}
	else if (h == 0) return 1;
	else {
		min = (u<m)?u:m;
		for (i=0;i<=min;i++) {
			espacos(t-h);
			printf("CALC = %ld C %ld x f(%ld,%ld,%ld)\n",min,i,u-i,min,h-1);
			sum += comb(u,i)*calcula(t,u-i,h-1,min);
			espacos(t-h+1);
			printf("SUM = %ld\n",i,sum);
		}
		espacos(t-h);
		printf("RESULT = %ld\n",sum);
	}
	return sum;
}


int main (long argc, char *argv[]) {
	long n_uces,n_horarios,max_conflitos;
	if (argc >= 4) {
		// tudo ok!
		n_uces = (long) atoi(argv[1]);
		n_horarios = (long) atoi(argv[2]);
		max_conflitos = (long) atoi(argv[3]);
		calcula(n_horarios,n_uces,n_horarios,max_conflitos);
	}
	else {
		fprintf(stderr,"Syntax:\n\t%s <N.º UCEs> <N.º Horarios> <N.º Max Conflitos>\n",argv[0]);	
	}
}


